// Import necessary modules
import Mock from 'mockjs'

// Function to convert URL parameters to an object
function param2Obj(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse(
        '{"' +
        decodeURIComponent(search)
            .replace(/"/g, '\\"')
            .replace(/&/g, '","')
            .replace(/=/g, '":"') +
        '"}'
    )
}

// Mocked device list data
let DeviceList = []
const deviceCount = 100

function randomMacAddress() {
    return "XX:XX:XX:XX:XX:XX".replace(/X/g, function () {
        return "0123456789ABCDEF".charAt(Math.floor(Math.random() * 16))
    });
}

for (let i = 0; i < deviceCount; i++) {
    // 生成类似 MAC 地址的随机字符串
    DeviceList.push(
        Mock.mock({
            id: Mock.Random.uuid(),
            name: Mock.Random.word(),
            macAddress: randomMacAddress(),
            online: Mock.Random.boolean(),
            ipAddress: Mock.Random.ip(),
            status: Mock.Random.integer(0, 1), // 0: 正常, 1: 故障
            type: Mock.Random.integer(0, 1) // 0: A类型设备, 1: B类型设备
        })
    )
}

export default {
    /**
     * Get device list
     * @return {{code: number, count: number, list: *[]}}
     */
    getDeviceList: config => {
        const { name, page = 1, limit = 20 } = param2Obj(config.url);

        const filteredList = DeviceList.filter(device => {
          if (name && (device.name.indexOf(name) === -1)) {
            return false;
          }
          return true;
        });
        
        const pageList = filteredList.slice((page - 1) * limit, page * limit);
        
        return {
          code: 20000,
          count: filteredList.length,
          list: pageList
        };
        
    },

    /**
     * Create a new device
     * @param name, macAddress, online, ipAddress, status, type
     * @return {{code: number, data: {message: string}}}
     */
    createDevice: config => {
        const { name, macAddress, online, ipAddress, status, type } = JSON.parse(config.body)

        DeviceList.unshift({
            id: Mock.Random.uuid(),
            name: name,
            macAddress: macAddress,
            online: online,
            ipAddress: ipAddress,
            status: status,
            type: type
        })

        return {
            code: 20000,
            data: {
                message: 'Device created successfully'
            }
        }
    },

    /**
     * Delete a device by ID
     * @param id
     * @return {{code: number, data: {message: string}}}
     */
    deleteDevice: config => {
        const { id } = JSON.parse(config.body)
        if (!id) {
            return {
                code: -999,
                message: 'Invalid parameters'
            }
        } else {
            DeviceList = DeviceList.filter(device => device.id !== id)
            return {
                code: 20000,
                data: {
                    message: 'Device deleted successfully'
                }
            }
        }
    },

    /**
     * Update a device
     * @param id, name, macAddress, online, ipAddress, status, type
     * @return {{code: number, data: {message: string}}}
     */
    updateDevice: config => {
        const { id, name, macAddress, online, ipAddress, status, type } = JSON.parse(config.body)

        DeviceList.some(device => {
            if (device.id === id) {
                device.name = name
                device.macAddress = macAddress
                device.online = online
                device.ipAddress = ipAddress
                device.status = status
                device.type = type
                return true
            }
        })

        return {
            code: 20000,
            data: {
                message: 'Device updated successfully'
            }
        }
    }
}
